package genetic.q4;

import java.util.LinkedList;
import java.util.List;

public class SolutionTreeNode {

	SolutionTreeNode parent;
	List<EternityPiece> solution;

	List<Integer> colors;
	List<Integer> extras;
	List<EternityPiece> forbidden;

	int fsize;
	int index;
	
	public SolutionTreeNode(SolutionTreeNode parent, List<EternityPiece> solution, int fsize, int index) {
		this.parent = parent;
		this.solution = solution;
		this.fsize = fsize;
		this.index = index;

		boolean flag = true;

		List<Integer> colors2 = new LinkedList<Integer>();
		List<Integer> extras2 = new LinkedList<Integer>();
		if (isSolution()){
			if (parent==null){
				colors = new LinkedList<Integer>();
				extras = new LinkedList<Integer>();
				for (int i=0; i<60; i++){
					colors.add(EternityPiece.GRAY);
				}
				for (int i=0; i<4; i++){
					extras.add(EternityPiece.GRAY);
				}
				forbidden = new LinkedList<EternityPiece>();
			} else{
				colors = parent.getColors();
				extras = parent.getExtras();
				forbidden = new LinkedList<EternityPiece>(parent.getForbidden());
				forbidden.addAll(solution);
			}

			int color1=0, color2=0;
			int corner = fsize/4;
			for (int i=0; i<fsize && flag; i++){
				if (i%corner == 0){
					color1 = colors.get(i);
					color2 = extras.get(i/corner);
				} else {
					EternityPiece p = solution.get(i-1);
					color1 = p.getColor(color1, 1, color2, 2);	//TODO problem if it has 2 colors of the same kinds
					color2 = colors.get(i);
					if (i%corner == 1){
						extras2.add(solution.get(i).getColor(color1, 1, color2, 3));
					} else {
						if (i==fsize-1){
							colors2.add(0, solution.get(i).getColor(color1, 1, color2, 3));
						} else {
							colors2.add(solution.get(i).getColor(color1, 1, color2, 3));
						}
											
					}
				}
			}
		}
		colors = colors2;
		extras = extras2;
	}

	public List<EternityPiece> getForbidden() {
		return forbidden;
	}

	public List<Integer> getExtras() {
		return extras;
	}

	public List<Integer> getColors() {
		return colors;
	}

	public boolean isSolution(){
		return solution.size()==fsize;
	}

	public List<EternityPiece> getSolution() {
		return solution;
	}
	
	public int getIndex(){
		return index;
	}
}
